package com.netty.server.processor;

import com.netty.server.cmd.ByteCmd;
import com.netty.server.processor.down.DownPlugin;
import com.netty.server.sender.CmdRequest;

import java.util.List;

/**
 * @author: 王霄
 * @date: 2022/2/18 19:18
 */
public class DefaultDownProcessor implements DownProcessor {

    List<? extends DownPlugin> downPlugins;

    public DefaultDownProcessor(List<? extends DownPlugin> downPlugins) {
        this.downPlugins = downPlugins;
    }

    @Override
    public ByteCmd processor(CmdRequest request) {
        return downPlugins.stream()
                .filter(x -> x.getCmd() == request.getCmd().getType())
                .findFirst()
                .orElseThrow(() -> new RuntimeException(String.format("[cmd = %s]未找到对应的下行处理插件", request.getCmd().getType())))
                .handler(request);
    }
}
